package data.nanjing.expo.services;

import data.nanjing.expo.models.dao.UserDao;
import data.nanjing.expo.models.domain.User;
import data.nanjing.expo.models.domain.dto.BaseUser;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;

/**
 * Created by zjp on 2018/6/30.
 * QQ：34948062
 * github: https://www.github.com/zjp1029
 * web： http://www.zjp1029.cn
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    //用户登录
    public User selectUserByPhoneNoAndPassword(String phoneNo, String password){
        return userDao.selectUseByPhoneNoAndPassword(phoneNo, encryptedPasswordBySha256(password));
    }

    //用户是否已注册
    public boolean isRegister(String phoneNo) {
        int count = userDao.selectCountByPoneNo(phoneNo);
        if (count == 0) {
            return false;
        }else {
            return true;
        }
    }

    //用户注册,添加用户
    public Integer addUser(User user) {
        user.setPassword(encryptedPasswordBySha256(user.getPassword()));
        return  userDao.addUserInfo(user);
    }

    //更新个人基本信息
    public void updatePeopleBaseInfo(BaseUser user) {
        userDao.updatePeopleBaseInfo(user);
    }

    public User selectUserInfoByPhoneNo(String phoneNo) {
        return userDao.selectUserInfoByPhoneNo(phoneNo);
    }

    //用户密码加密,使用sha256加密方式
    private String encryptedPasswordBySha256(String password) {
        String encryPassword = DigestUtils.sha256Hex(password);
        return encryPassword;
    }
   /* private String encryptedPasswordBySha256(String password) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(password.getBytes("UTF-8"));
            String encryPassword = Hex.encodeHexString(hash);
            return encryPassword;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }*/




}
